<?php
namespace Xiaoc\Api;


/**
 * Xiaoc商城店铺
 *
 * @author: zhy <zhy302813@163.com> 2017-8-19
 */

class Crowd extends Common {
        public function getRules() {
            return array(
                'getYplists' => array(
                    'page'     =>  array('name'=>'page','desc'=>'页数','default'=>1,'type'=>'int'),
                    ),
                'getGoodsDetail' => array(
                    'act_id'        => array('name' =>'act_id','desc'=>'活动ID','type'=>'int' ),
                    'ad_link'       => array('name' =>'ad_link','desc'=>'广告位ID','type'=>'int'),
                    'user_token'    =>  array('name'=>'user_token','desc'=>'用户token')
                ),
                'getCrowdLists'=>array(
                    'cat_id'        =>  array('name'=>'cat_id','desc'=>'分类ID'),
                    'type'          =>  array('name'=>'type','desc'=>'1最新2热门'),
                ),
                'get_crowd_users'   =>  array(
                    'act_id'   => array('name' =>'act_id','desc'=>'活动ID','type'=>'int','require'=>true),
                    'page'     =>  array('name'=>'page','desc'=>'页数','default'=>1,'type'=>'int'),
                ),
            );
	}


      /**
       * 校园汇优品
      * @desc校园汇优品列表
       */
      public function getYpLists(){
          //最新上架
          $where='';
          $where.=' a.is_on_sale=1 ';
          $where.=' AND a.is_delete=0 ';
          $where.=' AND b.shoprz_type=6 ';
          $order='';
          $order.=' a.add_time DESC ';
          $limit=6;
          $lists_new = getLists($where,$order,$limit);
          $data['news'] = $lists_new;
          //热门优品
          $order='';
          $order.=' a.click_count DESC ';
          $limit=6;
          $lists_hot = getLists($where,$order,$limit);
          $data['hots'] = $lists_hot;

          //优品列表
          $order='';
          $order.=' a.click_count DESC,a.add_time DESC ';
          $limit=6;
          $lists = getLists($where,$order,$limit);
          $data['normals'] = $lists;

          return $data;
           
           
      }
     
     /**
       * 校园汇众筹
      * @desc校园汇众筹列表
       */
      public function getCrowdLists(){
          $join['[>]goods'] = array('goods_id');
          $where['goods_activity.act_type'] =GAT_GROUP_BUY;
          $where['goods_activity.review_status'] = ACTSUCCESS;
          $where['goods_activity.start_time[<]'] =gmtime();
          $where['goods_activity.end_time[>]'] =gmtime();
          $cat_id = $this->cat_id;
          $type= $this->type;
          if ($cat_id){
              $cat_id = $this->medoo->select('category','cat_id',array('parent_id'=>$cat_id));
              $where['goods.cat_id'] = intval($cat_id);
          }
          if ($type && in_array($type,array('1','2'))){
              $where['LIMIT']=3;
              if ($type==1){
                  $where['goods_activity.is_new'] = 1;
              }elseif ($type==2){
                  $where['goods_activity.is_hot'] = 1;
              }
          }else{
              $where['LIMIT']=array($this->limitStart,$this->limitEnd);
          }
          $where['ORDER'] = array('goods_activity.act_id'=>'DESC');
          $field = array('goods_activity.act_name','goods_activity.ext_info','goods_activity.end_time','goods_activity.act_id','goods_activity.activity_thumb','goods.goods_thumb','goods.goods_id');
          $list =$this->medoo->select('goods_activity',$join,$field,$where);
          $count = 0;
          if (isset($list[0]['act_name'])){
              if (empty(intval($type))){
                  unset($where['LIMIT']);
                  unset($where['ORDER']);
                  $count = $this->medoo->count('goods_activity',$join,'goods_activity.act_id',$where);
              }
              $model = $this->medoo;
              $order_where['extension_code'] = 'group_buy';
              foreach ($list as $key=>$value){
                  $order_where['goods_id'] = $value['goods_id'];
                  $list[$key]['order_num'] = $model->sum('order_goods','goods_number',$order_where);
                  $res = unserialize($value['ext_info']);
                  $list[$key]['ext_info'] =$res;
              }
              $data['list'] = $list;
          }else{
              $data['list'] = array();
          }
          $data['hasmore'] = $this->HasMore($count);
          return $data;
      }


      /**
       * 校园汇盒子
      * @desc校园汇盒子列表
       */
      public function getBoxLists(){
          //最新上架
          $where='';
          $where.=' is_on_sale=1 ';
          $where.=' AND is_delete=0 ';
          $where.=' AND brand_id=18 ';
          $order='';
          $order.=' add_time DESC ';
          $limit=4;
          $lists_new = getBoxLists($where,$order,$limit);
          $data['news'] = $lists_new;
          //热门众筹
          $order='';
          $order.=' click_count DESC ';
          $limit=3;
          $lists_hot = getBoxLists($where,$order,$limit);
          $data['hots'] = $lists_hot;

          //众筹列表
          $order='';
          $order.=' click_count DESC,add_time DESC ';
          $limit=2;
          $lists = getBoxLists($where,$order,$limit);
          $data['normals'] = $lists;

          return $data;
           
           
      }


      /**
       * 校园汇盒子，众筹，优品详情页
      * @desc校园汇盒子列表
       */
      public function getGoodsDetail(){
          $ad_link = $this->ad_link;
          $act_id = $this->act_id;
          if (empty($ad_link) && empty($act_id)){
              return array();
          }
          $join['[>]goods'] = array('goods_id');
          $where['goods_activity.act_type'] =GAT_GROUP_BUY;
          $where['goods_activity.review_status'] = ACTSUCCESS;
          $where['goods_activity.start_time[<]'] =gmtime();
          $where['goods_activity.end_time[>]'] =gmtime();
          if ($ad_link){
              $where['goods_activity.gooods_id'] = $ad_link;
          }elseif ($act_id){
              $where['goods_activity.act_id'] = $act_id;
          }
          $field = array('goods_activity.act_name','goods_activity.ext_info','goods_activity.end_time','goods_activity.act_id','goods_activity.activity_thumb','goods.goods_thumb','goods_activity.goods_id','goods.market_price');
          $info =$this->medoo->get('goods_activity',$join,$field,$where);
          if (isset($info['act_name']) && $info['act_name']){
              $info['is_try'] = 0;
              $res = $this->medoo->get('goods_activity','act_id',array('act_type'=>GAT_TRY_GOODS,'goods_id'=>$info['goods_id']));
              if ($res){
                  $info['is_try'] = $res;
              }
              $order_where['extension_code'] = 'group_buy';
              $order_where['goods_id'] = $info['goods_id'];
              $info['order_num'] = $this->medoo->sum('order_goods','goods_number',$order_where);
              $info['is_collect'] = false;
              if ($this->user_token){
                  $user_info = $this->check_login($this->user_token);
                  $rec_id =  $this->medoo->get('collect_goods','rec_id',array('user_id'=>$user_info['user_id'],'goods_id'=>$info['goods_id']));
                  if ($rec_id){
                      $info['is_collect'] = true;
                  }
              }
          }
          $info['ext_info'] =  unserialize($info['ext_info']);
          return $info;
      }
       /**
       * 众筹参与者
       * @desc众筹参与者
        * @return  string user_name 用户名
        * @return  string user_picture 用户头像
        * @return  string add_time 参与时间
       */
      public function get_crowd_users(){
          $act_id = $this->act_id;
          $join['[>]order_goods'] = array('order_goods.goods_id'=>'goods_activity.goods_id');
          $join['[>]order_info'] = array('order_info.order_id'=>'order_goods.order_id');
          $join['[>]users'] = array('users.user_id'=>'order_info.user_id');
          $where['goods_activity.act_id'] = $act_id;
          $where['order_info.extension_code'] = 'group_buy';
          $where['goods_activity.act_type'] =GAT_GROUP_BUY;
          $where['goods_activity.review_status'] = ACTSUCCESS;
          $where['LIMIT']=array($this->limitStart,$this->limitEnd);
          $where['ORDER'] = array('order_info.add_time'=>'DESC');
          $where['GROUP'] = array('order_info.user_id');
          $field = array('users.user_name','users.user_picture','order_info.add_time');
          $list = $this->medoo->select('goods_activity',$join,$field,$where);
          $count = 0;
          if (isset($list[0]['user_name']) && $list[0]['user_name']){
              foreach ($list as $key=>$value){
                  $list[$key]['user_name'] = PassStart($value['user_name'],1,1);
                  $list[$key]['add_time'] = time_format($value['add_time'],'Y-m-d');
              }
              $data['list'] = $list;
              unset($where['LIMIT']);
              unset($where['ORDER']);
              unset($where['GROUP']);
              $count = $this->medoo->count('goods_activity',$join,'goods_activity.act_id',$where);
          }else{
              $data['list'] = array();
          }
          $data['hasmore'] = $this->HasMore($count);
          return $data;
      }

        
}
